﻿using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using xReplace;

namespace xReplaceUI
{
    public partial class MainView : Form
    {
        private string m_fileName = "";

        public MainView()
        {
            InitializeComponent();
            this.Text = "xReplace Shell " + this.GetType().Assembly.GetName().Version.Major + "." + this.GetType().Assembly.GetName().Version.Minor + " (C)2008-2010 Pixolüt Industries";
        }

        public void PrintStatus(int current, int max, bool s)
        {
            this.pbStatus.Maximum = max;
            this.pbStatus.Value = current;
            Application.DoEvents();
        }

        public void PrintOutput(string msg)
        {
            this.txOutput.AppendText(msg + "\n");
            Application.DoEvents();
        }

        public void PrintError(string msg)
        {
            this.txErrors.AppendText(msg + "\n");
            Application.DoEvents();
        }



        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.fdSource.ShowDialog(this) == DialogResult.OK)
            {
                this.m_fileName = this.fdSource.FileName;
                this.txOutput.AppendText("Project: " + this.m_fileName + "\n\n");
            }
        }

        private void executeScriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                xReplaceCore x = new xReplaceCore(new xReplaceCore.status(this.PrintStatus), new xReplaceCore.output(this.PrintOutput), new xReplaceCore.error(this.PrintError));
                this.executeScriptToolStripMenuItem.Enabled = false;
                InvocationOptions  o = new InvocationOptions();
                x.Run(this.m_fileName,o);
                this.executeScriptToolStripMenuItem.Enabled = true;
            }
            catch (Exception ex)
            {
                this.txErrors.AppendText(ex.Message);
            }

        }

        private void openManualToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                HelpWindow hw = new HelpWindow();
                hw.Show(this);
            }
            catch (Exception ex)
            {
                this.txErrors.AppendText(ex.Message);
            }
        }

        private void saveOutputAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.fdSave.Title = "Save Output Logs";
            this.fdSave.DefaultExt = "txt";
            this.fdSave.Filter = "xReplace Log File | *.txt";
            if (this.fdSave.ShowDialog(this) == DialogResult.OK)
            {
                try 
                {
                    File.WriteAllText(this.fdSave.FileName, this.txOutput.Text);                
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?","Confirm Quit",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Environment.Exit(0);
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("xReplace " + this.GetType().Assembly.GetName().Version.Major + "." + this.GetType().Assembly.GetName().Version.Minor + "\n(C)2008-2010 Pixolüt Industries\nWritten by Joe Cincotta", "About xReplace", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
